import {getInfo} from "@/api/login";


export const initMenu = (router,store)=>{
    if(store.state.routes.length>0){
        return;
    }
    getInfo().then(data=>{
        if(data){
            //格式化好的routers
            let fmtRoutes = formatRoutes(data.routes);
            //添加到路由
            router.addRoutes(fmtRoutes);
            //将数据存入vuex
            store.commit('initRoutes',fmtRoutes)
        }
    })
}

export const formatRoutes = (routes) =>{
     let formatRoute = [];
    routes.forEach(router=>{
        let{
            path,
            component,
            menuName,
            children,
            icon
        } = router;
        if(children && children instanceof Array){
            children = formatRoutes(children)
        }
        let fmtRouter = {
            icon:icon,
            path:path,
            name:menuName,
            children:children,
            component(resolve){
                require(['../views'+component+'.vue'],resolve);
            }
        }
        formatRoute.push(fmtRouter)
    })
    return formatRoute;
}
